﻿using Infragistics.Win.UltraMessageBox;
using Infragistics.Win.UltraWinGrid;
using OrBitDataModule.Dialog;
using OrBitDataModule;
using OrBitDataModule.Base;
using OrBitDataModule.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrBitDataModule.Contract;
using TouchSocket.Sockets;
using Infragistics.Win.UltraWinMaskedEdit;

namespace OrBitDataModule.Controls
{
    public partial class TaskManager : UserControl
    {
        public TaskManager()
        {
            InitializeComponent();
            InitTask();
        }
        private async void InitTask()
        {
            await GetAllModule();
            this.btn_addModule.Click += Btn_addModule_Click;
            this.btn_editModule.Click += Btn_editModule_Click;
            this.btn_removeModule.Click += Btn_removeModule_Click;
            this.ultraGrid_module.DoubleClickCell += UltraGrid_module_DoubleClickCell;
            btn_refresh.Click += Btn_refresh_Click;
        }

        private void Btn_addModule_Click(object sender, EventArgs e)
        {
            AddModule(string.Empty);
        }

        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="uniqueId"></param>
        private async void AddModule(string uniqueId)
        {
            if (!string.IsNullOrWhiteSpace(uniqueId))
            {
                var result = await TaskApi.GetTaskById(uniqueId);
                if (result.Result)
                {
                    TaskModel taskModel = result.TaskModel;
                    using (AddTask addTask = new AddTask(true, taskModel))
                    {
                        addTask.ShowDialog();
                        await GetAllModule();
                    }
                }
                else
                {
                    UltraMessageBoxManager.Show(this, $"没有找到项目，不允许修改！信息:{result.Message}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                using (AddTask addTask = new AddTask(false, null))
                {
                    addTask.ShowDialog();
                    await GetAllModule();
                }
            }
        }

        private void UltraGrid_module_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            string uniqueId = e.Cell.Row.GetCellValue("UniqueId").ToString();
            AddModule(uniqueId);
        }

        private async void Btn_removeModule_Click(object sender, EventArgs e)
        {
            UltraGridRow selectedRow = ultraGrid_module.ActiveRow;
            if (selectedRow != null)
            {
                string uniqueId = selectedRow.Cells["UniqueId"].Value?.ToString();
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    if (Common.LocalSetting.CheckConfigExists(uniqueId))
                    {
                        UltraMessageBoxManager.Show(this, "选中的项目已经被添加任务，请先在当前任务删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    if (UltraMessageBoxManager.Show(this, "确定要删除选中项？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        var result = await TaskApi.RemoveTask(uniqueId);
                        UltraMessageBoxManager.Show(this, result.Message, result.Result ? "提示" : "错误", MessageBoxButtons.OK, result.Result ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                        await GetAllModule();
                    }
                }
            }
        }

        private void Btn_editModule_Click(object sender, EventArgs e)
        {
            UltraGridRow selectedRow = ultraGrid_module.ActiveRow;
            if (selectedRow != null)
            {
                string uniqueId = selectedRow.Cells["UniqueId"].Value?.ToString();
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    AddModule(uniqueId);
                }
            }
        }
        private async void Btn_refresh_Click(object sender, EventArgs e)
        {
            await GetAllModule();
        }

        private async Task GetAllModule()
        {
            //var TaskList = await TaskApi.GetAllTaskAsync();
            var result = await TaskApi.GetAllTask();
            if (result.Result)
            {
                ultraGrid_module.DataSource = result.TaskList;
                ultraGrid_module.DataBind();
            }
        }
    }
}
